import { useMemo } from 'react'
import { useSelector } from 'react-redux'

function useWidget(widgetId) {
  const widgets = useSelector((state) => state.core.widgets)
  const selectedWidgetIds = useSelector((state) => state.core.selectedWidgetIds)
  const selectedWidget = useMemo(() => widgets[selectedWidgetIds[0]], [selectedWidgetIds, widgets])
  const currentWidget = useMemo(() => widgets[widgetId] || {}, [widgetId, widgets])

  return { widgets, selectedWidgetIds, selectedWidget, currentWidget }
}

export default useWidget
